package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.AppVariable;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.CommentInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/art")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request) {
        UserInfo userInfo = UserSessionUtils.getUserSession(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法参数");
        }
        List<ArticleInfo> list = articleService.getMyList(userInfo.getId());
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getContent().length() > 100) {
                list.get(i).setContent(list.get(i).getContent().substring(0, 80) + "......");
            }
        }

        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(HttpServletRequest request, Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数");
        }
        UserInfo userInfo = UserSessionUtils.getUserSession(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法参数");
        }
        int res = articleService.del(id, userInfo.getId());
        return AjaxResult.success(res);

    }

    @RequestMapping("/detail")
    public AjaxResult getDetail(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数");
        }
        return AjaxResult.success(articleService.getDetail(id));
    }

    @RequestMapping("/uprcount")
    public AjaxResult incrRCount(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数！");
        }

        return AjaxResult.success(articleService.incrRCount(id));
    }

    @RequestMapping("/add")
    public AjaxResult add(HttpServletRequest request, ArticleInfo articleInfo) {
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        UserInfo userInfo = UserSessionUtils.getUserSession(request);
        if (userInfo == null || userInfo.getId() <= 0) {
            return AjaxResult.fail(-2, "无效的用户参数！");
        }
        articleInfo.setUid(userInfo.getId());
        articleInfo.setContent(articleInfo.getContent().toString().replace("#", ""));
        return AjaxResult.success(articleService.add(articleInfo));
    }

    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo, HttpServletRequest request) {
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())
                || articleInfo.getId() == null) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        UserInfo userInfo = UserSessionUtils.getUserSession(request);
        if (userInfo == null || userInfo.getId() == null) {
            return AjaxResult.fail(-2, "无效用户!");
        }
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        articleInfo.setState(1);
        return AjaxResult.success(articleService.update(articleInfo));
    }

    /**
     * 查询列表根据分页
     *
     * @param pindex 当前页码
     * @param psize  每页显示的条数
     * @return
     */
    @RequestMapping("/listpage")
    public AjaxResult getListByPage(Integer pindex, Integer psize) {
        // 1.参数校验
        if (pindex == null || pindex <= 1) {
            pindex = 1;
        }
        if (psize == null || psize <= 1) {
            psize = 2;
        }
        // 偏移量
        int offset = (pindex - 1) * psize;
        List<ArticleInfo> list = articleService.getListByPage(psize, offset);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getContent().length() > 100) {
                list.get(i).setContent(list.get(i).getContent().substring(0, 80) + "......");
            }
        }
        return AjaxResult.success(list);
    }

    @RequestMapping("/artcount")
    public AjaxResult getArtCount() {
        return AjaxResult.success(articleService.getArtCount());
    }

    @RequestMapping("/draft")
    public AjaxResult draft(HttpServletRequest request, ArticleInfo articleInfo) {
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        UserInfo userInfo = UserSessionUtils.getUserSession(request);
        if (userInfo == null || userInfo.getId() <= 0) {
            return AjaxResult.fail(-2, "无效的用户参数！");
        }
        articleInfo.setUid(userInfo.getId());
        articleInfo.setContent(articleInfo.getContent().toString().replace("#", ""));
        articleInfo.setState(-1);
        return AjaxResult.success(articleService.draft(articleInfo));
    }

    @RequestMapping("/listdraft")
    public AjaxResult getListDraft(HttpServletRequest request) {
        UserInfo userInfo = UserSessionUtils.getUserSession(request);
        if (userInfo == null || userInfo.getId() <= 0) {
            return AjaxResult.fail(-2, "无效用户！");
        }

        return AjaxResult.success(articleService.getListDraft(userInfo.getId()));
    }

    /**
     * 获取评论列表 根据文章id
     *
     * @param artid
     * @return
     */
    @RequestMapping("/comment")
    public AjaxResult getCommentListByArtId(Integer artid) {
        if (artid == null || artid <= 0) {
            return AjaxResult.fail(-1, "非法参数");
        }

        return AjaxResult.success(articleService.getCommentListByArtId(artid));
    }

    @RequestMapping("/upcomment")
    public AjaxResult upComment(HttpServletRequest request, CommentInfo commentInfo) {
        if (commentInfo == null || commentInfo.getArtid() <= 0) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        HttpSession session = request.getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute(AppVariable.USER_SESSION_KEY);
        commentInfo.setCommentername(userInfo.getUsername());
        return AjaxResult.success(articleService.upComment(commentInfo));
    }
}
